﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.IO;

namespace JQueryControlToolkit
{
    public class DomResult : ScriptDescriptor, JSObject
    {
        private StringBuilder _script;
        private List<DomOperation> _operations;
        private List<string> _parameters;

        public DomResult()
        {
            _script = new StringBuilder();
            //_script.AppendLine("var e;");
            _operations = new List<DomOperation>();
        }

        public DomResult AddOperation(string operationType, object parameter, string code)
        {
            return AddOperation(operationType, new Dictionary<string, object>() { { String.Empty, parameter } }, code);
        }

        public DomResult AddOperation(string operationType, Dictionary<string, object> parameters, string code)
        {
            _script.AppendLine(code);
            Operations.Add(new DomOperation()
            {
                OperationType = operationType,
                Parameters = parameters,
                Code = code
            });
            return this;
        }

        public void NoParameter()
        {
            _parameters = new List<string>();
        }

        public void AddParameter(string parameterName)
        {
            if (_parameters == null)
            {
                NoParameter();
            }
            _parameters.Add(parameterName);
        }

        public List<DomOperation> Operations
        {
            get
            {
                return _operations;
            }
        }

        public static string Escape(string stringToEscape)
        {
            var serializer = new JavaScriptSerializer();
            return serializer.Serialize(stringToEscape);
        }


        public string ToScript()
        {
            return ToScript((string)null, true);
        }

        public string ToScript(string functionName, bool includeSemiColon)
        {
            StringBuilder toScript = new StringBuilder();
            StringWriter writer = new StringWriter(toScript);
            ToScript(writer, functionName, includeSemiColon);
            return toScript.ToString();
        }

        public void ToScript(TextWriter writer)
        {
            ToScript(writer, false);
        }

        public void ToScript(TextWriter writer, bool includeSemiColon)
        {
            ToScript(writer, null, includeSemiColon);
        }

        //public void ToScript(StringBuilder toScript)
        //{
        //    ToScript(toScript, null);
        //}

        internal void ToScript(TextWriter toScript, string functionName, bool includeSemiColon)
        {
            if (_parameters != null)
            {
                toScript.Write("function");
                toScript.Write(String.IsNullOrEmpty(functionName) ? String.Empty : " " + functionName);
                toScript.Write("(");
                toScript.Write(String.Join(",", _parameters.ToArray()));
                toScript.Write(") {");
            }

            foreach (DomOperation op in Operations)
            {
                op.ToScript(toScript);

                if (includeSemiColon && op == Operations[Operations.Count - 1])
                {
                    toScript.Write(";");
                }

                if (!op.Code.StartsWith("$") && !(op == Operations[Operations.Count - 1]))
                {
                    //toScript.Append(Environment.NewLine + "\t");
                }
            }

            if (_parameters != null)
            {
                toScript.Write("}");
            }
        }

        protected override string GetScript()
        {
            return ToScript();
        }
    }
}
